package cn.edu.xhu.library_management.area.service.impl;

import cn.edu.xhu.library_management.area.entity.LmInfo;
import cn.edu.xhu.library_management.area.mapper.InfoMapper;
import cn.edu.xhu.library_management.area.service.InfoService;
import cn.edu.xhu.library_management.common.entity.ResultCode;
import cn.edu.xhu.library_management.common.exception.LibraryManagementException;
import org.apache.ibatis.annotations.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: MMJ
 * @Date: 2022 - 11 - 12
 * @Description:
 */
@Service
public class InfoServiceImpl implements InfoService {
    @Resource
    private InfoMapper mapper;

    @Override
    public List<LmInfo> findAllInfos() {
        return mapper.findAllInfos();
    }

    @Override
    public LmInfo findInfoById(Integer id) {
        return mapper.findInfoById(id);
    }

    @Override
    public boolean addInfo(LmInfo info) {
        // 查询图书馆名称是否存在
        if (mapper.findInfoByName(info.getName()) != null){
            throw new LibraryManagementException(ResultCode.OPERATION_ERROR,"图书馆已经存在");
        }

        return mapper.addInfo(info) > 0;
    }

    @Override
    public boolean updateInfo(LmInfo info) {
        // 通过id查询图书馆是否存在，不存在，则抛异常
        if (findInfoById(info.getId()) == null){
            throw new LibraryManagementException(ResultCode.OPERATION_ERROR,"图书馆ID不存在");
        }
        return mapper.updateInfo(info) > 0;
    }

    @Override
    public boolean deleteInfoById(Integer id) {
        // 通过id查询图书馆是否存在，不存在，则抛异常
        if (findInfoById(id) == null){
            throw new LibraryManagementException(ResultCode.OPERATION_ERROR,"图书馆ID不存在");
        }
        return mapper.deleteInfoById(id) > 0;
    }
}
